package com.hghivln.blog.controller;


import com.hghivln.blog.common.lang.Result;
import com.hghivln.blog.common.lang.ResultEnum;
import com.hghivln.blog.common.lang.UserTypeEnum;
import com.hghivln.blog.entity.SysUser;
import com.hghivln.blog.pojo.dto.UpdatePwdDto;
import com.hghivln.blog.service.SysUserService;
import com.hghivln.blog.shiro.LoginToken;
import com.hghivln.blog.utils.ShiroUtils;
import com.hghivln.blog.pojo.vo.TokenVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author hghivln
 * @since 2021-09-01
 */
@RestController
@RequestMapping("sysUser")
public class SysUserController {

    @Autowired
    private SysUserService userService;

    /**
     * 登录
     */
    @PostMapping("doLogin")
    public Result<TokenVo> login(@RequestBody SysUser sysUser) {

        if (sysUser == null || StringUtils.isEmpty(sysUser.getUsername())||StringUtils.isEmpty(sysUser.getPassword())) {
            return new Result<>(ResultEnum.LOGIN_PARAM_ERROR);
        }
        LoginToken loginToken = new LoginToken(sysUser.getUsername(), sysUser.getPassword(), UserTypeEnum.SYS_USER);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(loginToken);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return new Result<>(ResultEnum.LOGIN_PARAM_ERROR);
        }
        String token = subject.getSession().getId().toString();
        return new Result<>(new TokenVo(token));
    }

    /**
     * 退出登录
     */
    @PostMapping("logout")
    public Result<?> logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new Result<>("退出成功！");
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("info")
    public Result<SysUser> info() {
        SysUser sysUser = userService.getById(ShiroUtils.getLoginUser().getId());
        //SysUser sysUser = ShiroUtils.getLoginUser();
        sysUser.setPassword(null);
        return new Result<>(sysUser);
    }

    /**
     * 获取管理员信息
     */
    @GetMapping("getSysUser")
    public Result<SysUser> getSysUser() {
        SysUser sysUser = userService.getById(1);
        return new Result<>(sysUser);
    }

    /**
     * 修改用户
     */
    @PutMapping("update")
    public Result<?> update(@RequestBody SysUser sysUser) {
        try {
            userService.updateById(sysUser);
            return new Result<>("修改成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("修改失败！");
        }
    }

    /**
     * 修改密码
     */
    @PutMapping("updatePwd")
    public Result<?> updatePwd(@RequestBody UpdatePwdDto updatePwdDto) {
        try {
            userService.updatePwd(updatePwdDto);
            return new Result<>("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(e.getMessage());
        }
    }


}
